/*
 *  Copyrigh 2009 Codeinn
 *  This program is distributed under the terms of the GNU General Public License
 *
 *  This file is part of Codeinn.
 *
 *  Codeinn is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  Codeinn is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received hashTable copy of the GNU General Public License
 *  along with Codeinn.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

package http.runnable;

import codeinn.CodeinnMIDlet;
import http.DJangoConnection;
import java.io.IOException;
import java.io.InputStream;
import javax.microedition.media.MediaException;

public class FetchVideo implements Runnable {

    private String videoURL = null;
    private DJangoConnection dJangoConnection;
    private CodeinnMIDlet codeinnMIDlet;

    public FetchVideo(CodeinnMIDlet codeinnMIDlet) {
        this.dJangoConnection = new DJangoConnection();
        this.codeinnMIDlet = codeinnMIDlet;
    }

    public void run() {
        try {
            InputStream inputStream = this.dJangoConnection.httpFetchVideo(this.videoURL);
            this.playVideo(inputStream);
        } catch(IOException ex) {
            ex.printStackTrace();
        } catch(MediaException ex) {
            ex.printStackTrace();
        } catch(IllegalArgumentException ex) {
            ex.printStackTrace();
        }
    }

    private void playVideo(InputStream inputStream) throws IOException, MediaException {
        System.out.println("Download done, start video playback");
        media.VideoPlayer videoPlayer = new media.VideoPlayer(inputStream);
        videoPlayer.play(this.codeinnMIDlet);
        if(inputStream != null) inputStream.close();
    }

    public void setVideoURL(String videoURL) {
        this.videoURL = videoURL;
    }
    
}
